Graph representing PCA.

library(datasets)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data(iris)

# Perform PCA
iris_pca <- prcomp(iris[,1:4], scale. = TRUE)
pca_data <- data.frame(PC1 = iris_pca$x[,1], 
                       PC2 = iris_pca$x[,2],
                       Species = iris$Species)

# Plot Iris data
base_fig <- plot_ly(x=iris[,1], y=iris[,2], z=iris[,3], type="scatter3d", mode="markers", color=iris$Species)
base_fig <- base_fig %>% layout(
  scene = list(
    xaxis = list(title = "Sepal Length"),
    yaxis = list(title = "Sepal Width"),
    zaxis = list(title = "Petal Length")
  ),
  title = "Iris Dataset"
)
base_fig
# Plot PCA results
ggplot(pca_data, aes(x = PC1, y = PC2, color = Species)) +
  geom_jitter(size = 3) +
  labs(title = "PCA of Iris Dataset", x = "Principal Component 1", y = "Principal Component 2") +
  theme_minimal()

iris[1:6,]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
pca_data[1:6,]
##         PC1        PC2 Species
## 1 -2.257141 -0.4784238  setosa
## 2 -2.074013  0.6718827  setosa
## 3 -2.356335  0.3407664  setosa
## 4 -2.291707  0.5953999  setosa
## 5 -2.381863 -0.6446757  setosa
## 6 -2.068701 -1.4842053  setosa

Graph of Discriminant Analysis

library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
# Perform LDA
iris_lda <- lda(Species ~ ., data = iris)

# Predict the LDA values
lda_pred <- predict(iris_lda)

# Create a data frame with LDA results
lda_data <- data.frame(LD1 = lda_pred$x[,1], 
                       LD2 = lda_pred$x[,2], 
                       Species = iris$Species)

# Plot Iris data
ggplot(iris, aes(x = iris[,1], y = iris[,2], color = Species)) +
  geom_jitter(size = 3) +
  labs(title = "Iris Dataset", x = "Sepal Length", y = "Sepal Width") +
  theme_minimal()

# Plot the LDA results
ggplot(lda_data, aes(x = LD1, y = LD2, color = Species)) +
  geom_jitter(size = 3) +
  labs(title = "LDA of Iris Dataset", x = "Linear Discriminant 1", y = "Linear Discriminant 2") +
  theme_minimal()

Now combine them

# Perform PCA
iris_pca <- prcomp(iris[,1:4], scale. = TRUE)
pca_data <- data.frame(PC1 = iris_pca$x[,1], 
                       PC2 = iris_pca$x[,2],
                       Species = iris$Species)

# Perform LDA
iris_pca_lda <- lda(Species ~ ., data = pca_data)

# Predict the LDA values
pca_lda_pred <- predict(iris_pca_lda)

# Create a data frame with LDA results
pca_lda_data <- data.frame(LD1 = pca_lda_pred$x[,1], 
                           LD2 = pca_lda_pred$x[,2], 
                           Species = iris$Species)

# Plot the PCA-LDA results
ggplot(pca_lda_data, aes(x = LD1, y = LD2, color = Species)) +
  geom_jitter(size = 3) +
  labs(title = "DACP (PCA->LDA) of Iris Dataset", x = "Linear Discriminant 1", y = "Linear Discriminant 2") +
  theme_minimal()